package com.aisile.crowd_funding.system.controller;

import com.aisile.crowd_funding.common.utils.MD5Utils;
import com.aisile.crowd_funding.common.utils.PageUtils;
import com.aisile.crowd_funding.common.utils.Query;
import com.aisile.crowd_funding.common.utils.R;
import com.aisile.crowd_funding.system.mapper.SysUserMapper;
import com.aisile.crowd_funding.system.pojo.SysRole;
import com.aisile.crowd_funding.system.pojo.SysUser;
import com.aisile.crowd_funding.system.service.RoleService;
import com.aisile.crowd_funding.system.service.UserService;
import excel.ExcelUtils;
import excel.POIConstant;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("sys/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @RequestMapping
    public String  user(){

        return "System/user/user";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,String> params){

       System.out.print(params);

        return userService.slectByPage(params);
    }

    @RequestMapping("exportUser")
    public void export(HttpServletResponse response) throws  Exception{
        //查询一个要导出的一个集合
        List<SysUser> sysUsers=userService.selectByExample(null);

        //2.起标题
        String title="用户列表";

        //3.起表头
        String[] headers={"用户ID","用户名","昵称","邮箱","联系方式"};
        //定义与表头相对应的字段第一个是值第二个是长度自适应
        Object[][] fields={
                {"userId", POIConstant.AUTO},
                {"username", POIConstant.AUTO},
                {"name", POIConstant.AUTO},
                {"email", POIConstant.AUTO},
                {"mobile", POIConstant.AUTO},
          };
        //5.定义规则
        ExcelUtils.ExportRules rules=new ExcelUtils.ExportRules(false,fields,title,headers,null);
        //6创建excel
         Workbook workbook= ExcelUtils.createWorkbook(sysUsers,rules);
        //7.设置响应参数
        response.reset();
        response.setContentType("application/vnd.ms-excel:charset=utf-8");
        //注意符号问题很重要 很重要 很重要 重要的事情说三遍 看好了！！！！！
        response.setHeader("Content-Disposition","attachment;filename="+new String(("测试.xlsx").getBytes(),"iso-8859-1"));
        //response.setHeader("Content-Disposition","attachment;filename="+ new String(("测试.xls").getBytes(), "iso-8859-1"));
        //8.输出到前台
        workbook.write(response.getOutputStream());


    }

    //跳转到添加
    @RequestMapping("/add")
    public String add(Model model){
       List<SysRole> list=roleService.selectByExample(null);
       model.addAttribute("list",list);
        return "System/user/add";
    }

    @RequestMapping("/hasUser")
    @ResponseBody
    public boolean hasUser(String username){
        SysUser user= userService.findByUserName(username);
        if(user==null){
            return true;
        }else{
            return false;
        }

    }
    @RequestMapping("save")
    @ResponseBody
    public R insert(SysUser sysUser){
        String p=sysUser.getPassword();
        sysUser.setPassword(MD5Utils.encrypt(p));
        int i=userService.insert(sysUser);

        if(i>0){
            return R.ok();
        }else{
            return R.error("添加失败");
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public R delete(Long id){
        int i=userService.deleteByPrimaryKey(id);
        if(i>0){
            return R.ok();
        }else{
            return R.error("删除失败");
        }
    }

    @RequestMapping("edit/{id}")
    public String edit(@PathVariable Long id,Model model){
         SysUser sysUser=userService.selectByPrimaryKey(id);
        List<SysRole> list=roleService.selectByExample(null);
        List<Long> lo=userService.getRoleIdList(id);
        if(lo!=null){
            model.addAttribute("role",lo);
        }
         model.addAttribute("user",sysUser);
         model.addAttribute("list",list);

        return "System/user/edit";
    }
    @RequestMapping("/update")
    @ResponseBody
    public R update(SysUser user){
        int i=userService.updateByPrimaryKey(user);
        if(i>0){
            return  R.ok();
        }else{
            return R.error("修改失败");
        }
    }

    @RequestMapping("/delall")
    @ResponseBody
    public R delAll(@RequestParam(value="ids[]" ) Long[] ids){
       int i=userService.delete(ids);
       if(i>0){
           return R.ok();

       }else {
           return R.error();
       }

    }
}
